package com.sky.controller;

import com.sky.domain.WmsPurchase;
import com.sky.service.WmsPurchaseService;
import com.sky.utils.PageUtils;
import com.sky.utils.R;
import com.sky.vo.param.WmsPurchaseCompleteVO;
import com.sky.vo.param.WmsPurchaseMergeVO;
import com.sky.vo.param.WmsPurchaseVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/purchase")
public class WmsPurchaseController {

    @Resource
    private WmsPurchaseService purchaseService;

    @GetMapping("/page")
    public R page(WmsPurchaseVO vo) {
        PageUtils page = purchaseService.pageQuery(vo);
        return R.ok(page);
    }

    @GetMapping("/getById/{id}")
    public R getById(@PathVariable("id") Long id) {
        WmsPurchase purchase = purchaseService.getById(id);
        return R.ok(purchase);
    }

    @PostMapping("/save")
    public R save(@RequestBody WmsPurchaseVO vo) {
        WmsPurchase purchase = new WmsPurchase();
        BeanUtils.copyProperties(vo, purchase);
        return purchaseService.save(purchase) ? R.ok() : R.error();
    }

    @PostMapping("/update")
    public R update(@RequestBody WmsPurchaseVO vo) {
        WmsPurchase purchase = new WmsPurchase();
        BeanUtils.copyProperties(vo, purchase);
        return purchaseService.updateById(purchase) ? R.ok() : R.error();
    }

    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] id) {
        return purchaseService.removeByIds(Arrays.asList(id)) ? R.ok() : R.error();
    }

    @GetMapping("/listUnReceivePurchase")
    public R listUnReceivePurchase() {
        List<WmsPurchase> wmsPurchases = purchaseService.listUnReceivePurchase();
        return R.ok(wmsPurchases);
    }

    @PostMapping("/merge")
    public R merge(@RequestBody WmsPurchaseMergeVO vo) {
        return purchaseService.merge(vo) ? R.ok() : R.error();
    }

    @PostMapping("/received")
    public R received(@RequestBody Long[] ids) {
        return purchaseService.received(ids) ? R.ok() : R.error();
    }

    @PostMapping("/complete")
    public R complete(@RequestBody WmsPurchaseCompleteVO vo) {
        return purchaseService.complete(vo) ? R.ok() : R.error();
    }
}
